import { createRouter } from '@tanstack/react-router'<% if (addOnEnabled['tanstack-query']) { %>
import { setupRouterSsrQueryIntegration } from '@tanstack/react-router-ssr-query'
import * as TanstackQuery from "./integrations/tanstack-query/root-provider";
<% } %>
<% if (addOnEnabled.sentry) { %>
import * as Sentry from "@sentry/tanstackstart-react";
<% } %>

// Import the generated route tree
import { routeTree } from './routeTree.gen'

// Create a new router instance
export const getRouter = () => {
<% if (addOnEnabled['tanstack-query']) { %>
    const rqContext = TanstackQuery.getContext();

    const router = createRouter({
        routeTree,
        context: { ...rqContext },
        defaultPreload: "intent",
        Wrap: (props: { children: React.ReactNode }) => {
          return (
            <TanstackQuery.Provider {...rqContext}>
              {props.children}
            </TanstackQuery.Provider>
          );
        },
      })

    setupRouterSsrQueryIntegration({router, queryClient: rqContext.queryClient})
<% } else { %>
  const router = createRouter({
    routeTree,
    scrollRestoration: true,
    defaultPreloadStaleTime: 0,
  })
<% } %>

<% if (addOnEnabled.sentry) { %>
  if (!router.isServer) {
    Sentry.init({
      dsn: import.meta.env.VITE_SENTRY_DSN,
      integrations: [],
      tracesSampleRate: 1.0,
      sendDefaultPii: true,
    });
  }
<% } %>

  return router;
}
